<?php

namespace plugin\admin\app\controller;

use app\services\RequestService;
use plugin\admin\app\model\PriceLog;
use support\Request;
use support\Response;
use plugin\admin\app\model\Order;
use plugin\admin\app\controller\Crud;
use support\exception\BusinessException;

/**
 * 订单列表 
 */
class OrderController extends Crud
{
    
    /**
     * @var Order
     */
    protected $model = null;

    /**
     * 构造函数
     * @return void
     */
    public function __construct()
    {
        $this->model = new Order;
    }

    public function select(Request $request): Response
    {
        $mobile = $request->get('mobile','');
        [$where, $format, $limit, $field, $order] = $this->selectInput($request);
        $where1=[];
        foreach ($where as $k => $v){
            if ($k=='id'){
                $where1['order.id'] = $v;
            }else{
                $where1[$k] = $v;
            }
        }
        $type = '';
        $count = count($where1);
        if ($count==1 && isset($where1['order.id'])){
            $type = 'detail';
        }
        $query = $this->doSelect($where1, $field, $order);
        $query = $query->from('wa_order as order')
            ->leftJoin('wa_yb_user as u','u.customer_uid','=','order.customer_id')->whereNotNull('order_date_time')->orderBy('order.order_create_time','asc')->select(['order.*','u.phone']);
        if (!empty($mobile)){
            $query = $query->where('u.phone','like','%'.$mobile.'%');
        }
        return $this->doFormat($query, $format, $limit,$type);
    }

    protected function doFormat($query, $format, $limit,$type=''): Response
    {
        $methods = [
            'select' => 'formatSelect',
            'tree' => 'formatTree',
            'table_tree' => 'formatTableTree',
            'normal' => 'formatNormal',
        ];
        $paginator = $query->paginate($limit);
        $total = $paginator->total();
        $items = $paginator->items();
        $total_price = PriceLog::where('appid','90F4F7FC5562A459D98F1C6B0FB82CF6')->value('price');
        if (method_exists($this, "afterQuery")) {
            $items = call_user_func([$this, "afterQuery"], $items);
        }
        // 查询订单详情
        if ($type == 'detail'){
//            $info = RequestService::orderInfo($items[0]['appid'],$items[0]['order_no']);

        }
        $format_function = $methods[$format] ?? 'formatNormal';
        return call_user_func([$this, $format_function], $items, $total,['total_price'=>$total_price]);
    }
    
    /**
     * 浏览
     * @return Response
     */
    public function index(): Response
    {
        return view('order/index');
    }

    /**
     * 插入
     * @param Request $request
     * @return Response
     * @throws BusinessException
     */
    public function insert(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::insert($request);
        }
        return view('order/insert');
    }

    /**
     * 更新
     * @param Request $request
     * @return Response
     * @throws BusinessException
    */
    public function update(Request $request): Response
    {
        if ($request->method() === 'POST') {
            return parent::update($request);
        }
        return view('order/update');
    }

}
